<?php
class Mail{
    /**
     * @desc  发送邮件
     * @param string $to                    邮件地址
     * @param string $title                 邮件主题
     * @param string $content               邮件内容
     * @param string $attachment            附件

    $a = \Mail::send( 'qq@qq.com' , 'xxx' , 'yyyyy' , '备份' );
     */
    public static function send( $to,$title,$content,$attachment=null ){
        if( !$to || !$title || !$content ){
            return [ 'status' => 0 , 'msg' => '参数不齐' ];
        }

        require_once  dirname( __FILE__ )."/SendMail.php";
        $EMAIL = [
            // 配置邮件发送服务器
            'MAIL_HOST' =>'smtp.mxhichina.com',//smtp服务器的名称
            'MAIL_FROM' =>'qq@qq.com',//发件人地址
            'MAIL_USERNAME' =>'qq@qq.com',//你的邮箱名
            'MAIL_PASSWORD' =>'qqqqqqqqqq',//邮箱密码
        ];


        $mail = new SendMail();
		$mail->setDebug(true); //是否输出调试信息
		// $mail->setServer("XXXXX", "XXXXX@XXXXX", "XXXXX"); //设置smtp服务器
		$mail->setServer($EMAIL['MAIL_HOST'], $EMAIL['MAIL_USERNAME'], $EMAIL['MAIL_PASSWORD'], 465, true); //设置smtp服务器，到服务器的ssl连接
		$mail->setFrom($EMAIL['MAIL_FROM']); //设置发件人
		$mail->setReceiver($to); //设置收件人，多个收件人，调用多次
		// $mail->setCc("XXXX"); //设置抄送，多个抄送，调用多次
		// $mail->setBcc("XXXXX"); //设置秘密抄送，多个秘密抄送，调用多次
		// $mail->addAttachment("XXXX.png"); //添加附件，多个附件，调用多次
		// $mail->addAttachment("XXXX.csv"); //添加附件，多个附件，调用多次 

		if($attachment){
            if(is_array($attachment)){
                foreach($attachment as $key=>$val){
                    if(file_exists($val)){
                        $mail->addAttachment($val); 
                    }
                }
            } else {
                if(file_exists($attachment)){
                    $mail->addAttachment($attachment); 
                }
            }
        }

		$mail->setMail($title, $content); //设置邮件主题、内容
		$res = $mail->send(); //发送

        if( $res ){
            return [ 'status' => 1 , 'msg' => '发送成功' ];
        } else {
            return [ 'status' => 0 , 'msg' => $mail->_errorMessage ];
        }
    }



    
}
?>